﻿
Partial Class App_Controls_PostWrite
    Inherits MyUI.UserControlBaseApp

    Protected CurrAccount As mDB.Account
    'Protected CurrMedia As mDB.Media

    Public Property PostText() As String
        Get
            Return Me.TxtPost.Text
        End Get
        Set(ByVal value As String)
            Me.TxtPost.Text = value
        End Set
    End Property
    Public Property Title() As String
        Get
            Return Me.LblTitle.Text
        End Get
        Set(ByVal value As String)
            Me.LblTitle.Text = value
        End Set
    End Property
    Public Property ReplyPostCode() As String
        Get
            Return Me.ViewState("ReplyPostCode")
        End Get
        Set(ByVal value As String)
            Me.ViewState("ReplyPostCode") = value
        End Set
    End Property

    Public Property ToAccount() As String
        Get
            Return Me.TxtToAccountName.Text
        End Get
        Set(ByVal value As String)
            Me.TxtToAccountName.Text = value
        End Set
    End Property
    Public Property PrivatePost() As Boolean
        Get
            Return Me.ChkPrivatePost.Checked
        End Get
        Set(ByVal value As Boolean)
            Me.ChkPrivatePost.Checked = value
        End Set
    End Property

    Public Property EnabledSendPost As Boolean
        Get
            Return Me.ViewState("EnabledSendPost")
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("EnabledSendPost") = value
        End Set
    End Property
    Public Property EnabledPrivate As Boolean
        Get
            Return Me.ViewState("EnabledPrivate")
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("EnabledPrivate") = value
        End Set
    End Property
    Public Property EnabledMedia As Boolean
        Get
            Return Me.ViewState("EnabledMedia")
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("EnabledMedia") = value
        End Set
    End Property
    Public Property EnabledToAccount As Boolean
        Get
            Return Me.ViewState("EnabledToAccount")
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("EnabledToAccount") = value
        End Set
    End Property
    Public Property MediaID() As String
        Get
            Return Me.HidImageID.Value
        End Get
        Set(ByVal value As String)
            Me.HidImageID.Value = value
        End Set
    End Property

    Public Event Send()

    Protected Sub New()

        Me.EnabledToAccount = False
        Me.EnabledMedia = True
        Me.EnabledSendPost = True
        Me.EnabledPrivate = False
        Me.AutoLoad = False

    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        CurrAccount = CurrUser.CurrAccount
        'SetCurrMedia(Me.MediaID)

        'If Not Me.IsPostBack Then
        '    SetDataPage()
        '    'Me.IsLoaded = True
        'End If


        'CurrMedia = Nothing
        'If Utils.Conv.ToInt(Me.HidImageID.Value) <> 0 Then
        '    CurrMedia = DB.TAB.Media.GetByPK(Me.HidImageID.Value)
        '    'Me.ImgPost.ImageUrl = "~/app/Mediaupload.aspx?id=" & Me.HidImageID.Value
        'End If

    End Sub

    Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender

        If Me.AutoLoad Then
            SetDataPage()
            Me.AutoLoad = False
        End If

    End Sub
    Public Function GetCurrMedia() As mDB.Media

        If Utils.Conv.ToInt(Me.MediaID) <> 0 Then
            Return DB.TAB.Media.GetByPK(Me.MediaID)
        Else
            Return Nothing
        End If



    End Function

    Public Sub SetDataPage()

        'Me.PostText = ""

        Me.BtnReply.Enabled = True
        Me.BtnReply.CssClass = "bottone-save"
        If Me.EnabledSendPost Then
            If Me.ReplyPostCode <> "" Then
                Me.BtnReply.Text = "Reply"
            Else
                Me.BtnReply.Text = "Send"

            End If
        Else
            Me.BtnReply.Visible = False
        End If

        Me.PnlPrivate.Visible = Me.EnabledPrivate

        Me.TxtToAccountName.Visible = Me.EnabledToAccount
        Me.HTML_Fileuploader.Visible = Me.EnabledMedia

        SetDataMedia()

    End Sub

    Public Sub SetDataMedia()

        Dim CurrMedia = GetCurrMedia()

        If CurrMedia IsNot Nothing Then
            Me.LblMediaName.Text = CurrMedia.Client_File_Name
            UI.Controls.ClassReplace(Me.HTML_Fileuploader, "a-icona-foto", "a-icona-upload-h")
            Me.BtnDeleteMedia.Visible = True
        Else
            UI.Controls.ClassReplace(Me.HTML_Fileuploader, "a-icona-upload-h", "a-icona-foto")
            Me.LblMediaName.Text = ""
            Me.BtnDeleteMedia.Visible = False

        End If

    End Sub

    Protected Sub BtnDeleteMadia_Click(sender As Object, e As EventArgs) Handles BtnDeleteMedia.Click


        DB.DeleteOnSubmit(GetCurrMedia())
        DB.SubmitChanges()
        Me.MediaID = 0


        SetDataMedia()



    End Sub

    Protected Sub BtnReply_Click(sender As Object, e As EventArgs) Handles BtnReply.Click

        'Dim tc = New WebApi.Twitter.TwitterClient(CurrAccount.getOauth)


        Dim res As Object = Nothing

        Dim net = MyApp.Network.AccTypeToNetType(CurrAccount.Network_Id)

        Dim post As mDB.Task_Post

        If Me.MediaID <> "" Then
            post = New mDB.Task_Post With {
                .Post_Text = Me.PostText,
                .Media = GetCurrMedia()
            }
        Else
            post = New mDB.Task_Post With {
                .Post_Text = Me.PostText, .Reply_Post_Code = Me.ReplyPostCode, .To_Account_Username = Me.ToAccount,
                .Post_Type_Id = IIf(Me.PrivatePost, mDB.Task_Post.Type.PRIVATE, mDB.Task_Post.Type.NORMAL)
            }
        End If


        Try

            res = MyApp.Task.Send.SendPost(CurrAccount, post)

        Catch ex As WebApi.Net.ExceptionAPI

            Dim aa = ex.Data

        End Try


        'Me.LblResult.Visible = True
        UI.Controls.ClassReplace(Me.BtnReply, "bottone-save", "bottone-mentions-a")

        If res Is Nothing Then
            Me.BtnReply.Text = "ERROR"

        Else
            Me.BtnReply.Text = "Sent!"

        End If
        Me.BtnReply.Enabled = False

        RaiseEvent Send()

    End Sub

    Protected Sub BtnReload_UpPPost_Click(sender As Object, e As EventArgs) Handles BtnReload_UpPPost.Click
        'SetCurrMedia(Me.HidImageID.Value)
        SetDataMedia()
    End Sub

End Class
